<?php


namespace App\Helpers\utils\WeChat;


use App\Models\Setting;
use Illuminate\Support\Facades\Http;

class AiSpeech
{
    var $urlPrefix = 'https://openai.weixin.qq.com/openapi';
    var $token = '';

    public function __construct()
    {
        $setting = Setting::where('key', 'ai_token')->first();
        $this->token = $setting['value'];
    }

    /**
     * 获取signature
     * @return \Illuminate\Http\Client\Response
     */
    public function sign()
    {
        $url = $this->urlPrefix . '/sign/' . $this->token;
        return Http::post($url, [
            'userid' => 1
        ]);
    }

    /**
     * 机器人回复
     * @param $text
     * @return \Illuminate\Http\Client\Response
     */
    public function bot($text)
    {
        $url = $this->urlPrefix . '/aibot/' . $this->token;
        return Http::post($url, [
            'signature' => checkSetting('ai_signature'),
            'query' => $text,
            'env' => 'debug'
        ]);
    }
}
